package cn.com.connected.base.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

/**
 * @author 邵明生
 */
@Slf4j
public class StringTools {
    //匹配大括号以及内部内容
    private static String patt = "\\{([^}]*)\\}";

    //
    public static String format(String input, Object... args){
        if (args==null || args.length==0){
            return String.valueOf(input);
        }
        input = input.replaceAll("\\{([^}]*)\\}","%s");
        input = String.format(input,args);
        return input;
    }

    /**
     * 驼峰字符串改为_分割
     * @return
     */
    public static String camel2underscore(String camelName){
        //先把第一个字母大写
        camelName = capitalize(camelName);

        String regex = "([A-Z][a-z]+)";
        String replacement = "$1_";

        String underscoreName = camelName.replaceAll(regex, replacement);
        //output: Pur_Order_Id_ 接下来把最后一个_去掉，然后全部改小写

        underscoreName = underscoreName.toLowerCase().substring(0, underscoreName.length()-1);

        return underscoreName;
    }

    /**
     * _分割转为驼峰字符串
     * @param underscoreName
     * @return
     */
    public static String underscore2camel(String underscoreName){
        String[] sections = underscoreName.split("_");
        StringBuilder sb = new StringBuilder();
        for(int i=0;i<sections.length;i++){
            String s = sections[i];
            if(i==0){
                sb.append(s);
            }else{
                sb.append(capitalize(s));
            }
        }
        return sb.toString();
    }

    /**
     * 首字母大写
     * @param str
     * @return
     */
    public static String capitalize(String str) {
        if(isEmpty(str)){
            return str;
        }
        return new StringBuilder(str.length())
                .append(Character.toUpperCase(str.charAt(0)))
                .append(str.substring(1))
                .toString();
    }

    /**
     * 首字母小写
     * @param str
     * @return
     */
    public static String lowerCase(String str) {
        if(isEmpty(str)){
            return str;
        }
        return new StringBuilder(str.length())
                .append(Character.toLowerCase(str.charAt(0)))
                .append(str.substring(1))
                .toString();
    }

    /**
     * 判断对象是否为Null或空
     * @param str
     * @return
     */
    public static boolean isEmpty(Object str) {
        return (str == null || "".equals(str));
    }

    /**
     * 判断字符串是否不为Null或空
     * @param str
     * @return
     */
    public static boolean isNotEmpty(Object str) {
        return !isEmpty(str);
    }

    /**
     * 字符串数组转成字符串
     * @param strs [a,b,c]
     * @return a,b,c
     */
    public static String toString(String[] strs){
        StringBuilder sb = new StringBuilder();
        for (String str : strs){
            sb.append(str+",");
        }
        String result = sb.toString();
        return result.substring(0, result.length() - 1);
    }

    /**
     * <p>Removes control characters (char &lt;= 32) from both
     * <pre>
     * StringUtils.trimToNull(null)          = null
     * StringUtils.trimToNull("")            = null
     * StringUtils.trimToNull("     ")       = null
     * StringUtils.trimToNull("abc")         = "abc"
     * StringUtils.trimToNull("    abc    ") = "abc"
     * </pre>
     *
     * @param str  the String to be trimmed, may be null
     * @return the trimmed String,
     *  {@code null} if only chars &lt;= 32, empty or null String input
     * @since 2.0
     */
    public static String trimToNull(String str){
        return StringUtils.trimToNull(str);
    }

    /**
     * <p>Strips whitespace from the start and end of a String  returning
     * {@code null} if the String is empty ("") after the strip.</p>
     *
     * <p>This is similar to {@link #trimToNull(String)} but removes whitespace.
     * Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
     *
     * <pre>
     * StringUtils.stripToNull(null)     = null
     * StringUtils.stripToNull("")       = null
     * StringUtils.stripToNull("   ")    = null
     * StringUtils.stripToNull("abc")    = "abc"
     * StringUtils.stripToNull("  abc")  = "abc"
     * StringUtils.stripToNull("abc  ")  = "abc"
     * StringUtils.stripToNull(" abc ")  = "abc"
     * StringUtils.stripToNull(" ab c ") = "ab c"
     * </pre>
     *
     * @param str  the String to be stripped, may be null
     * @return the stripped String,
     *  {@code null} if whitespace, empty or null String input
     * @since 2.0
     */
    public static String stripToNull(String str){
        return StringUtils.stripToNull(str);
    }



}